<template>
  <div class="home-layout">
    <!-- 顶部导航栏 -->
    <el-header class="app-header">
      <div class="header-content">
        <h1 class="logo">智慧养老</h1>
        <div class="user-avatar">
          <el-dropdown trigger="click" @command="handleCommand">
            <div class="avatar-wrapper">
              <el-avatar :src="user.avatar || defaultAvatar" />
              <span class="user-name">{{ baseUser.username || '未登录' }}</span>
              <el-icon class="arrow-down"><ArrowDown /></el-icon>
            </div>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="logout">
                  <el-icon><SwitchButton /></el-icon>退出登录
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
    </el-header>

    <!-- 主要内容区 -->
    <el-main class="app-main">
      <router-view></router-view>
    </el-main>

    <!-- 底部导航 -->
    <el-footer class="app-footer">
      <el-menu
          mode="horizontal"
          :default-active="activeMenu"
          router
          class="bottom-nav"
          @select="handleMenuSelect"
      >
        <el-menu-item index="/elderly/user-info" class="nav-item">
          <el-icon class="nav-icon"><User /></el-icon>
          <span class="nav-text">信息</span>
        </el-menu-item>
        <el-menu-item index="/elderly/social" class="nav-item">
          <el-icon class="nav-icon"><ChatDotRound /></el-icon>
          <span class="nav-text">社交</span>
        </el-menu-item>
        <el-menu-item index="/elderly/entertainment" class="nav-item">
          <el-icon class="nav-icon"><VideoPlay /></el-icon>
          <span class="nav-text">娱乐</span>
        </el-menu-item>
        <el-menu-item index="/elderly/medication" class="nav-item">
          <el-icon class="nav-icon"><FirstAidKit /></el-icon>
          <span class="nav-text">用药</span>
        </el-menu-item>
        <el-menu-item index="/elderly/activity" class="nav-item">
          <el-icon class="nav-icon"><FirstAidKit /></el-icon>
          <span class="nav-text">活动</span>
        </el-menu-item>
        <el-menu-item index="/elderly/emergency" class="nav-item">
          <el-icon class="nav-icon"><Bell /></el-icon>
          <span class="nav-text">紧急</span>
        </el-menu-item>
      </el-menu>
    </el-footer>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import {
  User, ChatDotRound, VideoPlay, FirstAidKit, Bell,
  ArrowDown, SwitchButton
} from '@element-plus/icons-vue'
import { ElMessageBox } from 'element-plus'

const route = useRoute()
const router = useRouter()
const defaultAvatar = ref('')

const userStr = localStorage.getItem('user');
const userData = JSON.parse(userStr);

const baseUser = ref(userData.user)
const user = ref({
  avatar: '',
  username: ''
})

// 计算当前激活的菜单项
const activeMenu = computed(() => {
  return route.path
})

const handleMenuSelect = (index) => {
  console.log('菜单选择:', index)
}

const handleCommand = (command) => {
  if (command === 'logout') {
    ElMessageBox.confirm('确定要退出登录吗?', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      // 清除本地存储
      localStorage.removeItem('user')
      //localStorage.removeItem('token')
      // 跳转到登录页
      router.push('/login')
    }).catch(() => {
      // 取消操作
    })
  }
}
</script>

<style scoped>
.home-layout {
  display: flex;
  flex-direction: column;
  height: 100vh;
  background-color: #f5f7fa;
}

/* 顶部导航栏样式 */
.app-header {
  height: 60px;
  background: linear-gradient(135deg, #4a6fa5 0%, #3a5a8f 100%);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  padding: 0 20px;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
  max-width: 1200px;
  margin: 0 auto;
}

.logo {
  margin: 0;
  font-size: 1.5rem;
  color: white;
  font-weight: 600;
  letter-spacing: 1px;
}

.user-avatar {
  display: flex;
  align-items: center;
}

.avatar-wrapper {
  display: flex;
  align-items: center;
  cursor: pointer;
  transition: all 0.3s;
  padding: 5px 10px;
  border-radius: 20px;
}

.avatar-wrapper:hover {
  background-color: rgba(255, 255, 255, 0.1);
}

.user-name {
  margin: 0 8px;
  font-size: 0.95rem;
  color: white;
}

.arrow-down {
  color: white;
  font-size: 12px;
  transition: transform 0.3s;
}

.el-dropdown:hover .arrow-down {
  transform: rotate(180deg);
}

/* 主要内容区样式 */
.app-main {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
  max-width: 1200px;
  width: 100%;
  margin: 0 auto;
}

/* 底部导航样式 */
.app-footer {
  padding: 0;
  background-color: white;
  box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.05);
}

.bottom-nav {
  display: flex;
  justify-content: space-around;
  max-width: 1200px;
  margin: 0 auto;
  border: none;
}

.nav-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 8px 5px;
  transition: all 0.3s;
  border: none !important;
}

.nav-item:hover {
  background-color: #f0f7ff;
}

.nav-item.is-active {
  color: #4a6fa5;
  background-color: #f0f7ff;
}

.nav-icon {
  font-size: 1.5rem;
  margin-bottom: 4px;
}

.nav-text {
  font-size: 0.85rem;
}

@media (max-width: 768px) {
  .app-main {
    padding: 10px;
  }

  .user-name {
    display: none;
  }

  .nav-text {
    font-size: 0.75rem;
  }
}
</style>